package imp;

import dao.TradingDao;
import model.Goods;
import model.Trading;
import util.DUBtil;

import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Trading_imp implements TradingDao {

    @Override
    public boolean InsertTrading(Trading trading) {
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        String sql = "insert into trading(id,sellerid,buyerid" +
                ",goodsid,date,state) values(?,?,?,?,?,?)";
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, trading.getId());
            pstmt.setString(2, trading.getSellerId());
            pstmt.setString(3, trading.getBuyerId());
            pstmt.setString(4, trading.getGoodsId());
            pstmt.setString(5, trading.getDate());
            pstmt.setString(6, trading.getState());
            pstmt.executeUpdate();
            con.close();
            pstmt.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean DeleteTrading(String TradingId) {
        Connection connection = new DUBtil().getConnection();
        PreparedStatement pstml = null;
        String sql = "delete from trading where id=?";
        try {
            pstml = connection.prepareStatement(sql);
            pstml.setString(1, TradingId);
            pstml.executeUpdate();
            connection.close();
            pstml.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean UpdateTrading(String  tradingid,String state) {
        Connection connection = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        String sql = "update trading set state=? where id=?";
        try {
            pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, state);
            pstmt.setString(2, tradingid);
            System.out.println("交易修改SQL语句:"+pstmt);
            pstmt.executeUpdate();
            pstmt.close();
            connection.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Trading judgetradinginfo(String GoodsId) {
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        Trading trading = new Trading();
        String sql = "select * from trading where goodsid=?";
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, GoodsId);
            System.out.println("交易记录查找SQL语句:"+pstmt);
        rs = pstmt.executeQuery();
            while (rs.next()) {
                trading.setId(rs.getString("id"));
            }
            rs.close();
            pstmt.close();
            con.close();
            return trading;
        } catch (SQLException e) {
            e.printStackTrace();
            return trading;
        }
    }
    @Override
    public Trading judgetrading(String Id) {
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        Trading trading = new Trading();
        String sql = "select * from trading where id=?";
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, Id);
            System.out.println("交易记录查找SQL语句:"+pstmt);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                trading.setId(rs.getString("id"));
                trading.setState(rs.getString("state"));
            }
            rs.close();
            pstmt.close();
            con.close();
            return trading;
        } catch (SQLException e) {
            e.printStackTrace();
            return trading;
        }
    }

    @Override
    public List<Trading> ShowTrading(String user_id) {
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "select \n" +
                "\tt1.id,\n" +
                "\tu1.id sellerid,\n" +
                "\tu1.name sellername,\n" +
                "\tu1.imgurl sellerimgurl,\n" +
                "\tu2.id buyerid,\n" +
                "\tu2.name buyername,\n" +
                "\tu2.imgurl buyerimgurl,\n" +
                "\tg1.id goodsid, \n" +
                "\tg1.imgurl goodsimgurl,\n" +
                "\t\tg1.name goodsname,\n" +
                "\tt1.date,\n" +
                "\tt1.state\n" +
                "from\n" +
                "\ttrading t1,\n" +
                "\tuser u1,\n" +
                "\tuser u2,\n" +
                "\tgoods g1\n" +
                "where \n" +
                "(t1.sellerid=u1.id and t1.buyerid=u2.id)\n" +
                "\tand t1.goodsid=g1.id and (t1.sellerid=? or t1.buyerid=?)\n" +
                "ORDER BY date DESC ";
        System.out.println("交易记录用户ID:"+user_id);
        List<Trading> list = new ArrayList<>();
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, user_id);
            pstmt.setString(2, user_id);
            System.out.println("交易记录查找SQL语句:"+pstmt);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Trading trading = new Trading();
                trading.setId(rs.getString("id"));
                trading.setSellerId(rs.getString("sellerid"));
                trading.setSellerName(rs.getString("sellername"));
                trading.setSellerImgurl(rs.getString("sellerimgurl"));
                trading.setBuyerId(rs.getString("buyerid"));
                trading.setBuyerName(rs.getString("buyername"));
                trading.setBuyerImgurl(rs.getString("buyerimgurl"));
                trading.setGoodsId(rs.getString("goodsid"));
                trading.setGoodsName(rs.getString("goodsname"));
                trading.setGoodsImgurl(rs.getString("goodsimgurl"));
                trading.setDate(rs.getString("date"));
                trading.setState(rs.getString("state"));
                list.add(trading);
            }
            rs.close();
            pstmt.close();
            con.close();
            return list;
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null,"数据库访问失败");
            return null;
        }
    }

    @Override
    public boolean DeleteGoodsTrading(String goodsid) {
        Connection connection = new DUBtil().getConnection();
        PreparedStatement pstml = null;
        String sql = "delete from trading where goodsid=?";
        try {
            pstml = connection.prepareStatement(sql);
            pstml.setString(1, goodsid);
            pstml.executeUpdate();
            connection.close();
            pstml.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}
